package declaraciones;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import main.Conexion;

import com.toedter.calendar.JDateChooser;

public class Inyeccion extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	private static Conexion conexion;
	
	private final String departamento = "Inyeccion";
	private JTextField textField_CodOperario;
	private JTextField textField_Nombre;
	private JTextField textField_Ref;
	
	private JButton btnGuardar;
	
	protected JDateChooser fecha;
	private JTextField textField_OK, textField_NG;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JTextField textField_10;
	private JTextField textField_11;
	private JTextField textField_12;

	public Inyeccion() {
		setLayout(null);
		
		conexion = new Conexion();
		conexion.open();
		
		JLabel lblCodigoOperario = new JLabel("Codigo Operario:");
		lblCodigoOperario.setBounds(10, 11, 104, 14);
		add(lblCodigoOperario);
		
		textField_CodOperario = new JTextField();
		textField_CodOperario.setBounds(130, 8, 86, 20);
		add(textField_CodOperario);
		textField_CodOperario.setColumns(10);
		
		textField_Nombre = new JTextField();
		textField_Nombre.setBounds(325, 8, 179, 20);
		add(textField_Nombre);
		textField_Nombre.setColumns(10);
		
		JLabel lblNombre = new JLabel("Nombre:");
		lblNombre.setBounds(252, 11, 63, 14);
		add(lblNombre);
		
		btnGuardar = new JButton("Guardar");
		btnGuardar.setBounds(243, 411, 89, 23);
		btnGuardar.addActionListener(this);
		add(btnGuardar);
		
		JLabel lblReferencia = new JLabel("Referencia:");
		lblReferencia.setBounds(10, 43, 79, 14);
		add(lblReferencia);
		
		textField_Ref = new JTextField();
		textField_Ref.setFont(new Font("Tahoma", Font.BOLD, 18));
		textField_Ref.setBounds(104, 33, 125, 28);
		add(textField_Ref);
		textField_Ref.setColumns(10);
		
		JLabel lblBuscarNombreEn = new JLabel("Buscar nombre en Base de Datos...");
		lblBuscarNombreEn.setBounds(252, 43, 235, 14);
		add(lblBuscarNombreEn);
		
		JLabel lblFecha = new JLabel("Fecha:");
		lblFecha.setBounds(10, 72, 46, 14);
		add(lblFecha);
		
		Date dt = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		fecha = new JDateChooser(cal.getTime());
		((JTextField) fecha.getDateEditor().getUiComponent()).setEnabled(false);
		fecha.setBounds(66, 72, 104, 14);
		add(fecha);
		
		JLabel lblPiezasOk = new JLabel("Piezas OK:");
		lblPiezasOk.setBackground(Color.GREEN);
		lblPiezasOk.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblPiezasOk.setBounds(26, 148, 125, 28);
		add(lblPiezasOk);
		
		textField_OK = new JTextField();
		textField_OK.setHorizontalAlignment(SwingConstants.CENTER);
		textField_OK.setFont(new Font("Tahoma", Font.BOLD, 15));
		textField_OK.setBounds(161, 146, 86, 38);
		add(textField_OK);
		textField_OK.setColumns(10);
		
		JLabel lblPiezasNg = new JLabel("Piezas NG:");
		lblPiezasNg.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblPiezasNg.setBounds(269, 148, 125, 28);
		add(lblPiezasNg);
		
		textField_NG = new JTextField();
		textField_NG.setHorizontalAlignment(SwingConstants.CENTER);
		textField_NG.setFont(new Font("Tahoma", Font.BOLD, 15));
		textField_NG.setColumns(10);
		textField_NG.setBounds(404, 146, 86, 38);
		add(textField_NG);
		
		JLabel lblCausasDefectos = new JLabel("Causas Defectos:");
		lblCausasDefectos.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblCausasDefectos.setBounds(26, 230, 144, 14);
		add(lblCausasDefectos);
		
		JCheckBox chckbxRechupes = new JCheckBox("Rechupes");
		chckbxRechupes.setBounds(26, 257, 97, 23);
		add(chckbxRechupes);
		
		textField_5 = new JTextField();
		textField_5.setHorizontalAlignment(SwingConstants.CENTER);
		textField_5.setBounds(130, 258, 46, 20);
		add(textField_5);
		textField_5.setColumns(10);
		
		JCheckBox checkBox = new JCheckBox("Rechupes");
		checkBox.setBounds(26, 283, 97, 23);
		add(checkBox);
		
		textField_6 = new JTextField();
		textField_6.setHorizontalAlignment(SwingConstants.CENTER);
		textField_6.setColumns(10);
		textField_6.setBounds(130, 284, 46, 20);
		add(textField_6);
		
		JCheckBox checkBox_1 = new JCheckBox("Rechupes");
		checkBox_1.setBounds(26, 309, 97, 23);
		add(checkBox_1);
		
		textField_7 = new JTextField();
		textField_7.setHorizontalAlignment(SwingConstants.CENTER);
		textField_7.setColumns(10);
		textField_7.setBounds(130, 310, 46, 20);
		add(textField_7);
		
		JCheckBox checkBox_2 = new JCheckBox("Rechupes");
		checkBox_2.setBounds(26, 335, 97, 23);
		add(checkBox_2);
		
		textField_8 = new JTextField();
		textField_8.setHorizontalAlignment(SwingConstants.CENTER);
		textField_8.setColumns(10);
		textField_8.setBounds(130, 336, 46, 20);
		add(textField_8);
		
		JCheckBox checkBox_3 = new JCheckBox("Rechupes");
		checkBox_3.setBounds(244, 257, 97, 23);
		add(checkBox_3);
		
		textField_9 = new JTextField();
		textField_9.setHorizontalAlignment(SwingConstants.CENTER);
		textField_9.setColumns(10);
		textField_9.setBounds(348, 258, 46, 20);
		add(textField_9);
		
		JCheckBox checkBox_4 = new JCheckBox("Rechupes");
		checkBox_4.setBounds(244, 283, 97, 23);
		add(checkBox_4);
		
		textField_10 = new JTextField();
		textField_10.setHorizontalAlignment(SwingConstants.CENTER);
		textField_10.setColumns(10);
		textField_10.setBounds(348, 284, 46, 20);
		add(textField_10);
		
		JCheckBox checkBox_5 = new JCheckBox("Rechupes");
		checkBox_5.setBounds(244, 309, 97, 23);
		add(checkBox_5);
		
		textField_11 = new JTextField();
		textField_11.setHorizontalAlignment(SwingConstants.CENTER);
		textField_11.setColumns(10);
		textField_11.setBounds(348, 310, 46, 20);
		add(textField_11);
		
		JCheckBox checkBox_6 = new JCheckBox("Rechupes");
		checkBox_6.setBounds(244, 335, 97, 23);
		add(checkBox_6);
		
		textField_12 = new JTextField();
		textField_12.setHorizontalAlignment(SwingConstants.CENTER);
		textField_12.setColumns(10);
		textField_12.setBounds(348, 336, 46, 20);
		add(textField_12);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnGuardar){
			Date dt = fecha.getDate();
			//java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
			String currentTime = sdf.format(dt);
			
			String query = "INSERT INTO produccion (codOperario, refArticulo, departamento, fecha, piezasOK, piezasNG)" +
						   "VALUES ("+Integer.parseInt(textField_CodOperario.getText())+",'"+textField_Ref.getText()+"','"+departamento+"','"+currentTime+"',"+Integer.parseInt(textField_OK.getText())+","+Integer.parseInt(textField_NG.getText())+")";
			conexion.ejecutarConsulta(query);
			
			String queryUpdate = "UPDATE articulos SET piezasOK = piezasOK + "+Integer.parseInt(textField_OK.getText())+", piezasNG = piezasNG + "+Integer.parseInt(textField_NG.getText())+", Cantidad = Cantidad + "+Integer.parseInt(textField_OK.getText())+" + "+Integer.parseInt(textField_NG.getText())+" WHERE Referencia = '"+textField_Ref.getText()+"'";
			conexion.ejecutarConsulta(queryUpdate);
			
			JDialog dialog = new JDialog();
			dialog.setVisible(true);
			dialog.getContentPane().setLayout(new BorderLayout());
			dialog.getContentPane().add(new JLabel("Datos Guardados!"),BorderLayout.CENTER);
			//dialog.pack();
			dialog.setModal(true);
			dialog.setMinimumSize(new Dimension(100, 100));
			dialog.setLocationRelativeTo(null);
		}
		
	}
}
